//
//  AY_UseListPage.m
//  AiYue
//
//  Created by boWen on 2023/11/21.
//

#import "AY_UseListPage.h"
#import "AY_UseListCell.h"
#import "PGDatePickManager.h"

@interface AY_UseListPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) NSMutableArray * dataAry;
@property (nonatomic, strong) UIButton * dataBtn;
@end

@implementation AY_UseListPage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithNav];
}

- (void)initWithNav {
    self.gk_navTitle = @"消费记录";
    [self.view addSubview:self.baseTableView];
    self.view.backgroundColor = COLOR_Bg;
    [self getConfigWithData:[self backCurrentDate]];
    [self setupHeaderView];

}


- (void)getConfigWithData:(NSString *)datetime {
    if(self.dataAry.count > 0) {
        [self.dataAry removeAllObjects];
    }
    NSString *logType = @"";
    if (self.logType == consumptionLog_bookBean) {
        logType = @"1";
    }else if (self.logType == consumptionLog_goldBean){
        logType = @"5";
        self.gk_navTitle = @"金币记录";
    }else if (self.logType == consumptionLog_vip){
        logType = @"2";
    }else if (self.logType == consumptionLog_money){
        logType = @"4"; //
    }
    [AYApiRequest getUserAssetRecordListWithAssetType:logType month:datetime success:^(AY_UserAssetModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            self.dataAry = [NSMutableArray arrayWithArray: pkg.data];
            [self.baseTableView reloadData];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, GK_STATUSBAR_NAVBAR_HEIGHT, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT - GK_STATUSBAR_NAVBAR_HEIGHT)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = [UIColor clearColor];
        [myTableView registerClass:[AY_UseListCell class] forCellReuseIdentifier:@"AY_UseListCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (NSArray *)dataAry {
    if(!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

- (void)setupHeaderView {
    UIView * timeView = [AYTool createViewWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, YNUM(44)) andBg:COLOR_Bg andRadius:YNUM(16) andAddSubView:self.view];
   self.dataBtn  = [AYTool createButtonWithFrame:CGRectZero andTitle:[self backCurrentDate] andFont:[UIFont boldSystemFontOfSize:12] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(dataPickerAction) andRadius:YNUM(16) andAddSubView:timeView];
    [self.dataBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(YNUM(100));
        make.height.offset(YNUM(32));
        make.left.equalTo(timeView).offset(YNUM(12));
        make.top.equalTo(timeView).offset(YNUM(6));
    }];
    
    self.baseTableView.tableHeaderView = timeView;
}

- (NSString *)backCurrentDate {
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
    NSString *str = [formatter stringFromDate:date];
    return str;
}


- (void)dataPickerAction {
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.textColorOfSelectedRow = THEME_COLOR;
    datePicker.lineBackgroundColor = THEME_COLOR;
    datePickManager.confirmButtonTextColor = THEME_COLOR;

    datePickManager.datePicker.datePickerMode = PGDatePickerModeYearAndMonth;
    Weak_Self
    datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
        NSString * seleStr = [NSString stringWithFormat:@"%ld-%ld", dateComponents.year, dateComponents.month];
        [weakSelf.dataBtn setTitle:seleStr forState:UIControlStateNormal];
        [weakSelf getConfigWithData:seleStr];
    };
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_UseListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_UseListCell"];
    cell.model = self.dataAry[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(70);
}


@end
